#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include "../include/client.h"

#define BUFFER_SIZE 1024

// 客户端主函数
int main(int argc, char *argv[]) {
    const char *server_ip = "127.0.0.1"; // 默认服务器IP
    int port = 8888; // 默认端口号
    
    // 解析命令行参数
    if (argc > 1) {
        server_ip = argv[1];
    }
    if (argc > 2) {
        port = atoi(argv[2]);
    }
    
    // 初始化客户端
    ChatClient *client = init_client();
    if (!client) {
        fprintf(stderr, "客户端初始化失败\n");
        return 1;
    }
    
    // 连接服务器
    if (!connect_to_server(client, server_ip, port)) {
        cleanup_client(client);
        return 1;
    }
    
    // 显示帮助信息
    show_help();
    
    // 主循环：处理用户输入
    char buffer[BUFFER_SIZE];
    while (client->connected) {
        // 提示用户输入
        printf("请输入命令: ");
        fflush(stdout);
        
        // 读取用户输入
        if (fgets(buffer, BUFFER_SIZE, stdin) == NULL) {
            break; // 用户中断输入
        }
        
        // 去除换行符
        size_t len = strlen(buffer);
        if (len > 0 && buffer[len - 1] == '\n') {
            buffer[len - 1] = '\0';
        }
        
        // 创建一个副本用于处理
        char processed_buffer[BUFFER_SIZE];
        strncpy(processed_buffer, buffer, BUFFER_SIZE - 1);
        processed_buffer[BUFFER_SIZE - 1] = '\0';
        
        // 检查是否以斜杠开头，如果是则去掉斜杠
        if (processed_buffer[0] == '/') {
            memmove(processed_buffer, processed_buffer + 1, strlen(processed_buffer));
        }
        
        // 检查是否以@用户名开头的私聊消息格式
        if (processed_buffer[0] == '@') {
            // 提取接收者用户名
            char *receiver_start = processed_buffer + 1;
            char *content_start = strstr(receiver_start, " ");
            
            if (!content_start) {
                printf("私聊格式错误，请使用 @用户名 <消息内容>\n");
                continue;
            }
            
            char receiver[50];
            char content[1024];
            
            // 提取接收者
            strncpy(receiver, receiver_start, content_start - receiver_start);
            receiver[content_start - receiver_start] = '\0';
            strcpy(content, content_start + 1);
            
            send_message(client, content, receiver);
            continue;
        }
        
        // 解析命令
        char command[50];
        sscanf(processed_buffer, "%s", command);
        
        // 处理命令
        if (strcmp(command, "exit") == 0 || strcmp(command, "quit") == 0) {
            // 退出客户端
            break;
        } else if (strcmp(command, "login") == 0) {
            // 登录命令
            char username[50], password[50];
            if (sscanf(processed_buffer, "%s %s %s", command, username, password) == 3) {
                client_login(client, username, password);
            } else {
                printf("参数格式错误，请使用 login <用户名> <密码> 或 /login <用户名> <密码>\n");
            }
        } else if (strcmp(command, "register") == 0) {
            // 注册命令
            char username[50], password[50];
            if (sscanf(processed_buffer, "%s %s %s", command, username, password) == 3) {
                client_register(client, username, password);
            } else {
                printf("参数格式错误，请使用 register <用户名> <密码> 或 /register <用户名> <密码>\n");
            }
        } else if (strcmp(command, "public") == 0) {
            // 公聊消息
            char content[1024];
            if (sscanf(processed_buffer, "%s %[^\n]", command, content) < 2) {
                // 尝试另一种方式解析
                char *content_start = strstr(processed_buffer, " ");
                if (content_start) {
                    send_message(client, content_start + 1, NULL);
                } else {
                    printf("请输入消息内容\n");
                }
            } else {
                send_message(client, content, NULL);
            }
        } else if (strcmp(command, "private") == 0) {
            // 私聊消息
            char receiver[50], content[1024];
            if (sscanf(processed_buffer, "%s %s %[^\n]", command, receiver, content) < 3) {
                // 尝试另一种方式解析
                char *receiver_start = strstr(processed_buffer, " ");
                if (receiver_start) {
                    char *content_start = strstr(receiver_start + 1, " ");
                    if (content_start) {
                        strncpy(receiver, receiver_start + 1, content_start - receiver_start - 1);
                        receiver[content_start - receiver_start - 1] = '\0';
                        send_message(client, content_start + 1, receiver);
                    } else {
                        printf("请输入消息内容\n");
                    }
                } else {
                    printf("参数格式错误，请使用 private <接收者> <消息内容> 或 @<接收者> <消息内容>\n");
                }
            } else {
                send_message(client, content, receiver);
            }
        } else if (strcmp(command, "online") == 0) {
            // 查看在线用户
            if (send(client->socket_fd, "online\n", strlen("online\n"), 0) == -1) {
                perror("发送命令失败");
            }
        } else if (strcmp(command, "history") == 0) {
            // 查看聊天历史
            if (send(client->socket_fd, "history\n", strlen("history\n"), 0) == -1) {
                perror("发送命令失败");
            }
        } else if (strcmp(command, "help") == 0) {
            // 显示帮助信息
            show_help();
        } else {
            // 其他命令直接发送给服务器
            strcat(processed_buffer, "\n");
            if (send(client->socket_fd, processed_buffer, strlen(processed_buffer), 0) == -1) {
                perror("发送命令失败");
            }
        }
    }
    
    // 清理客户端资源
    cleanup_client(client);
    
    printf("客户端已退出\n");
    
    return 0;
}
